using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SharpCast;
using SongsDB;
using System.Runtime.InteropServices;

namespace mmPodcasts
{
    [ClassInterface(ClassInterfaceType.AutoDual), ComVisible(true)]
    public partial class OptionsSheet : UserControl
    {
        private PodcastsPlugin plugin;
        private SDBApplication SDB;
        private List<Channel> newChannels = new List<Channel>();
        private List<Channel> removedChannels = new List<Channel>();

        public OptionsSheet()
        {
            InitializeComponent();
        }

        private void refreshChannels() {
            channelList.Items.Clear();
            foreach (Channel channel in plugin.getDirectory().channels) {
                if (!removedChannels.Contains(channel))
                {
                    ListViewItem item = channelList.Items.Add(channel.title);
                    item.Tag = channel;
                    item.SubItems.Add(channel.link.ToString());
                }
            }

            foreach (Channel channel in newChannels)
            {
                ListViewItem item = channelList.Items.Add(channel.title);
                item.Tag = channel;
                item.SubItems.Add(channel.link.ToString());
            }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            NewChannelDialog ncd = new NewChannelDialog();
            DialogResult result = ncd.ShowDialog();
            if (result == DialogResult.OK)
            {
                Channel channel = ncd.getChannel();
                if (channel != null)
                {
                    newChannels.Add(channel);
                    refreshChannels();
                }
            }
        }

        public void init(PodcastsPlugin podcastsPlugin, SDBApplication SDB)
        {
            this.plugin = podcastsPlugin;
            this.SDB = SDB;
            refreshChannels();

            autodownload.Text = SDB.IniFile.get_IntValue("Podcasts", "AutoDownload").ToString();
            updateFrequency.Text = SDB.IniFile.get_IntValue("Podcasts", "UpdateFrequency").ToString();
            deleteListened.Checked = SDB.IniFile.get_BoolValue("Podcasts", "KeepListened");
        }

        public void save()
        {
            channelList.Select();
            foreach (ListViewItem item in channelList.Items)
            {
                Channel channel = (Channel)item.Tag;
                channel.title = item.Text;

                if (removedChannels.Contains(channel))
                    plugin.getDirectory().removeChannel(channel);
                if (newChannels.Contains(channel))
                    plugin.getDirectory().addChannel(channel);
            }

            int tmp;
            if (int.TryParse(autodownload.Text, out tmp)) {
                SDB.IniFile.set_IntValue("Podcasts", "AutoDownload", tmp);
            }
            if (int.TryParse(updateFrequency.Text, out tmp))
            {
                SDB.IniFile.set_IntValue("Podcasts", "UpdateFrequency", tmp);
                plugin.restartTimer();
            }
            SDB.IniFile.set_BoolValue("Podcasts", "DeleteListened", deleteListened.Checked);
        }

        private void removeButton_Click(object sender, EventArgs e)
        {
            // TODO: warn user about removal

            foreach (ListViewItem item in channelList.SelectedItems)
            {
                Channel channel = (Channel) item.Tag;
                if (newChannels.Contains(channel))
                {
                    newChannels.Remove(channel);
                }
                else
                {
                    removedChannels.Add(channel);
                }
            }
            refreshChannels();
        }

        private void channelList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (channelList.SelectedItems.Count > 0)
            {
                Channel channel = (Channel)channelList.SelectedItems[0].Tag;
                desc.Text = channel.title + "\n" + channel.description;
            }
            else
            {
                desc.Text = "";
            }
        }
    }
}
